<?php
namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\logic\MemberLogic;

class Member extends AdminController
{
    public function initialize(){
        $this->modelName = 'Member';
        $this->controllerName = '会员';
        parent::initialize();
    }

    public function index()
    {
        /**
         * 查询条件
         */
        $whereKeys = [
            'is_follow' => ['key'=>'is_follow','op'=>'='],
            'is_black' => ['key'=>'is_black','op'=>'='],
            'keyword' => ['key'=>'keyword','op'=>'LIKE','fields'=>'mobile|realname|nickname'],
            'time_start' => ['key'=>'time_start','op'=>'>=','fields'=>'create_time','valueMethod'=>'strtotime'],
            'time_end' => ['key'=>'time_end','op'=>'<=','fields'=>'create_time','valueMethod'=>'strtotime'],
        ];

        $alj = new MemberLogic($this->modelName);
        $paramWhere = $this->getParamWhere($whereKeys);

        $pagerInfo = $this->getPagerInfo($alj->_getCountByWhere($paramWhere['where']),input('p',1),$paramWhere['search']);
        $dataList = $alj->_getListByWhere($paramWhere['where'],$pagerInfo['offset'],$pagerInfo['pageSize']);

        $this->assign('datas',$dataList);
        $this->assign('search',$paramWhere['search']);
        $this->assign('whereKeys',$whereKeys);
        $this->assign($pagerInfo);

        $this->assign('memberEnum',config('site_enum.member'));

        return $this->fetch();
    }

    public function edit()
    {
        $param = input('param.');
        $alj = new MemberLogic($this->modelName);

        if (request()->isAjax()){
            if(empty($param['id'])){
                $this->error('id参数错误');
            }
            $param['id'] = hashIdDecode($param['id']);
            if(empty($param['id'])){
                $this->error('id参数错误');
            }

            $flag = $alj->save($param);

            if($flag['err_code']  === 0){
                $this->success($flag['msg'] . '，返回列表',url('index'));
            }
            else{
                $this->error($flag['msg']);
            }
        }

        $id = input('param.id');
        if (empty($id)){
            $this->error('参数错误');
        }
        $id = hashIdDecode($id);
        if (empty($id)){
            $this->error('参数错误');
        }

        $chaxun = $alj->getModelByPk($id);
        if($chaxun['err_code'] !== 0){
            return json($chaxun);
        }
        $model = $chaxun['data'];
        $pkName = db($this->modelName)->getPk();
        $model[$pkName] = hashIdEncode($model[$pkName]);
        $this->assign([
            'model' => $model,
        ]);

        $this->assign('memberEnum',config('site_enum.member'));

        return $this->fetch();
    }
}
